<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class user_controller extends admin_controller
{
    // Get list
    public function __construct()
    {
        parent::__construct();
        if (!$this->is_admin_logged()) redirect(base_url() . $this->base_admin_url . 'login_controller');
        $this->page_title = 'User';
        $this->table_name = 'user';
        $this->route_name = 'user_controller';
        $this->business_model = $this->user_model;
    }

    public function index()
    {
        $header_data = array();
        $header_data['show_menu'] = true;
        $view_data = $this->get_view_data();
        $view_data['list'] = $this->get_list();
        $view_data['actions'] = $this->get_list_actions();
        $this->load_view('list_template', $header_data, $view_data);
    }

    public function delete()
    {
        if ($this->input->post('selected') === false) redirect(base_url() . $this->base_admin_url . $this->route_name);
        $selected = $this->input->post('selected');
        foreach ($selected as $id)
        {
            // Delete news
            if($this->get_logged_admin_account() != $id)
            $this->business_model->delete_data(array('id' => $id));
        }
        $this->session->set_userdata('message', 'Hoàn tất: Bạn đã xóa dữ liệu!');
        redirect(base_url() . $this->base_admin_url . $this->route_name);
    }

    public function get_list()
    {
        $list = array();
        if ($this->current_user_can('view_all_user')) $news = $this->business_model->get_elements();
        else $news = $this->business_model->get_elements_by_user_group_code($this->current_authority);

//        echo json_encode($news);exit;
        foreach ($news as $item)
        {
            $actions = array();
            $actions[] = array(
                'text' => 'Sửa',
                'href' => base_url() . $this->base_admin_url . $this->route_name . '/update/' . $item->id
            );

            $list[] = array(
                'can_delete' => $item->id,
                'id' => $item->id,
                'usernmae' => $item->username,
                'user_group_code' => $item->user_group_code,
                'note' => $item->note,
                'modified_date' => $item->modified_date,
                'actions' => $actions
            );
        }
        return $list;
    }

    public function get_header_list()
    {
        $list = array();
        $list[] = 'ID';
        $list[] = 'User Name';
        $list[] = 'User group code';
        $list[] = 'Note';
        $list[] = 'Modified Date';
        $list[] = 'Hành động';
        return $list;

    }

    public function update($id)
    {
        if ($this->current_user_can('add_new_admin')
            || (
                $this->current_user_can('add_new_diamond_staff')
                    && $this->business_model->get_count_staff_number(user_model::$diamond_type) < 7)
        )
        {
            $header_data = array();
            $header_data['show_menu'] = true;
            $view_data = $this->get_view_data();
            $view_data['fields'] = $this->get_fields($id);
            $view_data['actions'] = $this->get_form_actions($id);
            $this->load_view('form_template', $header_data, $view_data);
        }
        else
            redirect(base_url() . $this->base_admin_url . 'user_controller');

    }

    public function get_list_actions()
    {
        $actions = array();
        if ($this->current_user_can('add_new_admin')
            || (
                $this->current_user_can('add_new_diamond_staff')
                    && $this->business_model->get_count_staff_number(user_model::$diamond_type) < 12)
        )
            $actions[] = array(
                'text' => 'Thêm',
                'href' => base_url() . $this->base_admin_url . $this->route_name . '/update/-1',
                'view_change_method' => 'redirect'
            );
        $actions[] = array(
            'text' => 'Xóa',
            'href' => base_url() . $this->base_admin_url . $this->route_name . '/delete',
            'view_change_method' => 'submit'
        );
        return $actions;
    }

    public function get_post_data()
    {
        $this->business_model->username = $this->input->post('username');
        $this->business_model->password = $this->input->post('password');
        $this->business_model->user_group_code = $this->input->post('user_group_code');
        $this->business_model->note = $this->input->post('note');
        $this->business_model->modified_date = time();
        $this->business_model->authorities = $this->current_authority;

        if ($this->current_user_can('add_new_admin'))
            $this->business_model->user_group_code = $this->input->post('user_group_code');
        else if ($this->current_user_can('add_new_diamond_staff'))
            $this->business_model->user_group_code = user_model::$diamond_type;
    }

    public function get_fields($id)
    {
        if ($id == -1)
            $model = new $this->business_model();
        else
            $model = $this->business_model->get_element_by_id($id);

        $fields = array();

        $fields[] = array(
            'text' => 'User Name',
            'name' => 'username',
            'value' => $model->username,
            'type' => 'text'
        );

        $fields[] = array(
            'text' => 'Password',
            'name' => 'password',
            'value' => $model->password,
            'type' => 'text'
        );

        if ($this->current_user_can('add_new_admin'))
            $fields[] = array(
                'text' => 'User Group Code',
                'name' => 'user_group_code',
                'value' => $model->user_group_code,
                'type' => 'select',
                'options' => array(
                    array_to_object(array(
                        'text' => 'Super Admin',
                        'value' => user_model::$admin_type
                    )),
                    array_to_object(array(
                        'text' => 'Diamond Staff',
                        'value' => user_model::$diamond_type
                    ))
                ),
                'select_display' => 'text',
                'select_value' => 'value'
            );

        $fields[] = array(
            'text' => 'Note',
            'name' => 'note',
            'value' => $model->note,
            'type' => 'text'
        );
        return $fields;
    }

}
